Stygian_Debug_Active = Stygian_Debug_Active or nil
Stygian_Debug_Categories = Stygian_Debug_Categories or { }

function Stygian_Debug(category,debugData)
   if category then
      if table.contains(Stygian_Debug_Categories, category) then
         if type(debugData) == "table" then
            secho("<red>DEBUG " .. category .. ":<white>")
            display(debugData)
         elseif type(debugData) == "string" or type(debugData) == "number" then
            secho("<red>DEBUG " .. category .. ":<white> " .. debugData )
         else
            secho("<red>DEBUG " .. category .. ":<white> " .. tostring(debugData) )
         end
      end
   else
      if type(debugData) == "table" then
         secho("<red>DEBUG:<white>")
         display(debugData)
      elseif type(debugData) == "string" or type(debugData) == "number" then
         secho("<red>DEBUG:<white> " .. debugData)
      else
         secho("<red>DEBUG:<white> " .. tostring(debugData))
      end
   end
end

function SDebug(category, debugData)
   if not Stygian_Debug_Active then return end
   Stygian_Debug(category, debugData)
end

function Stygian_toggleDebug()
   if Stygian_Debug_Active then Stygian_Debug_Active = nil
   else Stygian_Debug_Active = true
   end
   secho("Debugging is currently " .. (( Stygian_Debug_Active and "<green>ON<white>") or "<red>OFF<white>"))
end

function Stygian_watchCategory( category )
   if table.contains(Stygian_Debug_Categories, category) then
      for i,v in ipairs(Stygian_Debug_Categories) do
         if v == category then
            table.remove(Stygian_Debug_Categories, i)
         end
      end
      secho("No longer watching the '<red>"..category.."<white>' category.") 
   else
      table.insert(Stygian_Debug_Categories, category)
      secho("Now watching the '<red>"..category.."<white>' category.")
   end
   secho("Debugging is currently " .. (( Stygian_Debug_Active and "<green>ON<white>") or "<red>OFF<white>"))
end

function Stygian_listCategories()
   if #Stygian_Debug_Categories > 0 then
      secho("You are currently watching the following categories:\n" .. table.concat(Stygian_Debug_Categories,", ") )
   else
      secho("You are not watching any debugs.")
   end
end

--[[ Alias for this:
   
   pattern: ^debug(?: (list))?$
   script:
   
      if matches[2] then
         Stygian_listCategories()
      else
         Stygian_toggleDebug()
      end

   pattern: ^debugc(?: (.*))?$
   script:
      if matches[2] then
         Stygian_watchCategory( matches[2] )
      else
         Stygian_listCategories()
      end
]]
